package com.lhf.novel.sysuser.po;

import com.lhf.novel.sysuser.security.enums.LoginEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * LoginPo实体类
 *
 * @author zy 刘会发
 * @since 2020/3/9$
 */
@ApiModel("用户登录实体")
@AllArgsConstructor
@NoArgsConstructor
@Data
public class LoginPo implements UserDetails {
    /**
     * 主键
     */
    @ApiModelProperty("用户id")
    private Integer id;
    /**
     * 用户名
     */
    @ApiModelProperty("用户名")
    private String username;
    /**
     * 密码
     */
    @ApiModelProperty("用户密码")
    private String password;
    /**
     * 真实姓名
     */
    @ApiModelProperty("真实姓名")
    private String realName;
    /**
     * 电子邮件
     */
    @ApiModelProperty("电子邮件")
    private String email;
    /**
     * 头像
     */
    @ApiModelProperty("头像")
    private String headImg;
    /**
     * 联系方式
     */
    @ApiModelProperty("联系方式")
    private String phone;
    /**
     * 是否锁定 0未锁定 1已锁定
     */
    @ApiModelProperty("是否锁定 0未锁定 1已锁定")
    private Integer isLock;
    /**
     * 0未删除 1已删除
     */
    @ApiModelProperty("0未删除 1已删除")
    private Integer isDel;

    @ApiModelProperty("用户角色")
    private List<LoginRolePo> roles;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.getRoles().stream().map(item -> new SimpleGrantedAuthority(item.getRole())).collect(Collectors.toList());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.getIsLock() == LoginEnum.IS_LOCK.getCode();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
